package com.wunong.smart.address.domain.operation.impl;

import com.wunong.smart.address.domain.UserAddressDomain;
import com.wunong.smart.address.application.context.UserAddressContext;
import com.wunong.smart.address.domain.operation.AbstractUserAddressOperation;
import com.wunong.smart.address.application.request.SetDefaultUserAddressRequest;
import com.wunong.smart.domain.platform.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 设为默认用户地址
 *
 * @author create by zealot.zt
 */
@Slf4j
@Component
public class SetDefaultUserAddressOperation extends AbstractUserAddressOperation<SetDefaultUserAddressRequest> {

    @Override
    protected Object doOperate(UserAddressContext<SetDefaultUserAddressRequest> context) {
        UserAddressDomain domain = context.getDomain();

        domain.setIsDefault(true);
        domain.setGmtModified(TimeUtils.asLocalDateTime(new Date()));

        // 重置用户地址是否默认
        userAddressRepository.setDefault(domain);
        log.warn("finish set default user address, userId:{}, addressId:{}",
                domain.getUserId(), domain.getId());
        return domain.getId();
    }

    @Override
    public Class<SetDefaultUserAddressRequest> getRequestClass() {
        return SetDefaultUserAddressRequest.class;
    }

}
